define([
    "helpers/network",
    "helpers/util",
    "views/login",
    "views/menus/profile",
    "views/menus/sidebar",
    "models/products",
    "models/roles",
    "libs/xhr"
], function (Network, Util, LoginView,  ProfileView, SidebarView, ProductsModel, RolesModel) {
    if (!sessionStorage.username) {
        return LoginView
    }

    webix.DataDriver["form-data"] = webix.copy(webix.DataDriver.json);
    webix.i18n.locales["zh-CN"] = {
        calendar: {
            monthFull: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            monthShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            dayFull: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
            dayShort: ["日", "一", "二", "三", "四", "五", "六"],
            hours: "小时",
            minutes: "分",
            done: "选择",
            clear: "清除",
            today: "今天"
        }
    };
    webix.i18n.setLocale("zh-CN");

    /**定义基本函数 开始*/

    String.prototype.toApiUrlImageSrcContent = function () {
        return this.replace(/src="[a-z.:\/0-9]*\/file\/ueditor/g, 'src="' + Network.url.imgUrl + '/file/ueditor').replace(/src="[a-z.:\/0-9]*\/file\/public\/uploads\/ueditor/g, 'src="' + Network.url.imgUrl + '/file/public/uploads/ueditor');
    };

    String.prototype.toPercent = function () {
        if (!this) {
            return 0
        }
        return (parseFloat(this) * 0.0001).toFixed(4);
    };

    String.prototype.toCurrency = function () {
        if (!this) {
            return 0
        }
        return (parseFloat(this) * 0.01).toFixed(2);
    };

    String.prototype.toCent = function () {
        if (!this) {
            return 0
        }
        return (parseFloat(this) * 100).toFixed();
    };

    Number.prototype.toPercent = function () {
        return (this * 0.0001).toFixed(4);
    };

    Number.prototype.toCurrency = function () {
        if (!this) {
            return 0
        }
        return (this * 0.01).toFixed(2);
    };

    Number.prototype.toIntNumber = function () {
        if (!this) {
            return 0
        }
        return (this * 0.01).toFixed();
    };

    Number.prototype.toCent = function () {
        return (this * 100).toFixed();
    };
    /**定义基本函数 结束*/

    webix.protoUI({
        name: "myFormTemplate",
        getValue: function () {
            return this.getValues();
        },
        setValue: function (values) {
            this.setValues(values);
        }
    }, webix.ui.template);

    webix.protoUI({
        name: "daterangepicker",
        getValue: function () {
            var t = this.s.type
                , e = "time" == t
                , i = (this.config.timepicker,
                this.s.value);
            if (this.s.stringResult) {
                var s = webix.i18n.parseFormatStr;
                return e && (s = webix.i18n.parseTimeFormatStr),
                !this.Hx || "month" != t && "year" != t || (s = this.Hx),
                    this.kE(s, i)
            }
            // return i || null
            if(this.config.timepicker){
                return {
                    start: i.start ? Util.getFormattedDateTime("yyyy-MM-dd hh:mm:ss", i.start) : null,
                    end: i.end ? Util.getFormattedDateTime("yyyy-MM-dd hh:mm:ss", i.end) : null
                }
            } else {
                return {
                    start: i.start ? Util.getFormattedDateTime("yyyy-MM-dd 00:00:00", i.start) : null,
                    end: i.end ? Util.getFormattedDateTime("yyyy-MM-dd 23:59:59", i.end) : null
                }
            }
        }
    }, webix.ui.daterangepicker);


    // RolesModel.fetchData();
    // ProductsModel.fetchData();
    // supplier.fetchData();


    //Top toolbar
    var mainToolbar = {
        view: "toolbar",
        elements: [
            {
                view: "label",
                label: "<a href='http://www.wd310.com'><img class='photo' src='assets/imgs/logo.png' /></a>",
                width: 200
            },
            {
                height: 46,
                id: "person_template",
                css: "header_person",
                borderless: true,
                width: 180,
                data: {
                    id: 3,
                    name: sessionStorage.getItem("userDisplayName") == "undefined" ? "未登录" : sessionStorage.getItem("userDisplayName")
                },
                template: function (obj) {
                    var html = "<div style='height:100%;width:100%;' onclick='webix.$$(\"profilePopup\").show(this)'>";
                    html += "<img class='photo' src='assets/imgs/photos/" + obj.id + ".png' /><span class='name'>" + obj.name + "</span>";
                    html += "<span class='webix_icon fa-angle-down'></span></div>";
                    return html;
                }
            },
            {},
            // {view: "icon", value: 5, icon: "search", width: 45, popup: "searchPopup"},
            // {view: "icon", icon: "envelope-o", value: 3, width: 45, popup: "mailPopup"},
            {id: "iconId", value: 0, view: "icon", icon: "bullhorn", width: 45, popup: "messagePopup"},
            // {view: "icon", icon: "comments-o", value: 5, width: 45, popup: "messagePopup"}
        ]
    };

    var body = {
        rows: [
            // {
            //     height: 49,
            //     id: "title",
            //     css: "title",
            //     template: "<div class='header'>#title#</div><div class='details'>( #details# )</div>",
            //     data: {text: "", title: ""}
            // },
            {
                view: "scrollview", scroll: "native-y",
                body: {cols: [{$subview: true}]}
            }
        ]
    };

    var layout = {
        rows: [
            mainToolbar,
            {
                cols: [
                    SidebarView,
                    body,
                ]
            }
        ]
    };

    return {
        $ui: layout,
        $menu: "app:SidebarView",
        $oninit: function (view, scope) {
            // message.getData(1);
            // scope.ui(search.$ui);
            // scope.ui(mail.$ui);
            // scope.ui(message.$ui);
            scope.ui(ProfileView.$ui);
        }
    };
});